package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.port.ISpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private ISpecService specService;

    @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupById(@RequestParam("id")Long cid){

        return ResponseEntity.ok(this.specService.queryGroupByCategory(cid));
    }

    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "gid",required = false)Long gid,
            @RequestParam(value = "cid",required = false)Long cid,
            @RequestParam(value = "searching",required = false)Boolean searching
            ){

        return ResponseEntity.ok(this.specService.querySpecParams(gid,cid,searching));
    }

    @PostMapping("group")
    public ResponseEntity<Void> addGroup(@RequestBody SpecGroupDTO specGroupDTO){
        System.out.println("controller:"+specGroupDTO);
        this.specService.addGroup(specGroupDTO);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    /**
     * 查询规格参数组，及组内参数
     * @param id 商品分类id
     * @return 规格组及组内参数
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecsByCid(@RequestParam("id") Long id){
        return ResponseEntity.ok(specService.querySpecsByCid(id));
    }

}
